package main

import (
	"fmt"
	"html/template"
	"net/http"
)

type Student struct {
	Name string
	Age  int
}

func hello(w http.ResponseWriter, r *http.Request) {
	//定义自定义函数
	myFun := func(name string) (string, error) {
		return "Hello " + name, nil
	}
	//定义模板
	tmpl := template.New("hello.tmpl")
	//绑定函数
	tmpl.Funcs(template.FuncMap{
		"hello": myFun,
	})
	//解析模板
	_, err := tmpl.ParseFiles("./hello.tmpl", "./common.tmpl")
	if err != nil {
		fmt.Println("创建模板异常, err:", err)
		return
	}
	//写入模板数据
	tmpl.Execute(w, "zhangyu")
}

func main() {
	http.HandleFunc("/", hello)
	err := http.ListenAndServe(":8088", nil)
	if err != nil {
		fmt.Println("HTTP服务器异常,err:", err)
		return
	}
}
